/*
 * Autor : Cmop
 */

/*
 * Ventana.java
 *
 * Created on 07/01/2013, 10:38:09 AM
 */
package validardni;

import conectarWeb.BusquedaCookie;
import conectarWeb.PeticionCookie;
import java.awt.Image;
import java.net.HttpCookie;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

/**
 *
 * @author jestrada
 */
public class Ventana extends javax.swing.JDialog {

    private List<Object> buscarCookieLogueo;

    /** Creates new form Ventana */
    public Ventana(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblImagen = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblRpta = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CONSULTAR NOMBRES");

        lblImagen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblImagen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblImagen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jLabel1.setText("DNI");

        lblRpta.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton1.setText("VALIDAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
        });

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(lblImagen, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButton1)))
                            .addComponent(lblRpta, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblRpta, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    jLabel2.setText("Consultando ...");
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {            
            StringBuilder sb_parametros = new StringBuilder();
            sb_parametros.append("accion=buscar&");
            sb_parametros.append("tecla_7=6&");
            sb_parametros.append("tecla_8=7&");
            sb_parametros.append("tecla_9=9&");
            sb_parametros.append("tecla_4=0&");
            sb_parametros.append("tecla_5=1&");
            sb_parametros.append("tecla_6=2&");
            sb_parametros.append("tecla_1=3&");
            sb_parametros.append("tecla_2=8&");
            sb_parametros.append("tecla_3=4&");
            sb_parametros.append("tecla_0=5&");
            sb_parametros.append("nuDni=");
            sb_parametros.append(jTextField1.getText().trim());
            sb_parametros.append("&");
            sb_parametros.append("imagen=");
            sb_parametros.append(jTextField2.getText().trim());
            sb_parametros.append("&");
            sb_parametros.append("bot_consultar.x=13&");
            sb_parametros.append("bot_consultar.y=24");
            String parametros = sb_parametros.toString();
            StringBuilder sb_url = new StringBuilder();
            sb_url.append("https://cel.reniec.gob.pe/valreg/valreg.do;");
            List<HttpCookie> cookies = (List<HttpCookie>) buscarCookieLogueo.get(0);
            for (HttpCookie httpCookie : cookies) {
                if (httpCookie.getName().compareToIgnoreCase("jsessionid") == 0) {
                    sb_url.append(httpCookie.getName());
                    sb_url.append("=");
                    sb_url.append(httpCookie.getValue());
                }
            }
            try {
                String buscarCookieLogueo1 = new PeticionCookie().peticionConCookieString(sb_url.toString(), "POST", parametros, (List<HttpCookie>) buscarCookieLogueo.get(0));
                String s2 = new String(buscarCookieLogueo1.getBytes(),"ISO-8859-1");
                lblRpta.setText(buscarCookieLogueo1);
                jLabel2.setText("Consulta Terminada");
            } catch (Exception ex) {
                jLabel2.setText("Error al procesar Consulta");
                ex.printStackTrace(System.out);
            }
        }
    });
}//GEN-LAST:event_jButton1ActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
    jTextField2.requestFocus();
}//GEN-LAST:event_jTextField1ActionPerformed

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
    jButton1.requestFocus();
}//GEN-LAST:event_jTextField2ActionPerformed

private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton1KeyPressed
    jButton1.doClick();
    lblRpta.requestFocus();
}//GEN-LAST:event_jButton1KeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Ventana Principal = new Ventana(new javax.swing.JFrame(), true);
                Principal.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                try {
                    Principal.buscarCookieLogueo = new BusquedaCookie().buscarCookieLogueo("https://cel.reniec.gob.pe/valreg/valreg.do?accion=ini");
                    Image buscarCookieLogueo1 = new PeticionCookie().peticionConCookieImagen("https://cel.reniec.gob.pe/valreg/codigo.do", "POST", "", (List<HttpCookie>) Principal.buscarCookieLogueo.get(0));
                    Principal.lblImagen.setIcon(new ImageIcon(buscarCookieLogueo1));

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                Principal.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel lblImagen;
    private javax.swing.JLabel lblRpta;
    // End of variables declaration//GEN-END:variables
}
